//
//  PageView.m
//  textReader
//
//  Created by kyocooro on 2/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PageView.h"


@implementation PageView
@synthesize textView;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
	CGContextRef context = UIGraphicsGetCurrentContext();
	switch (self.tag) {
		case KCURRENT_PAGE:
			[textView drawSubRect:context inRect:rect];
			break;
		case KPREV_PAGE:
		{
			int currentcStart = [textView getStart];
			[textView scrollPage:Page_Up]; //try to draw prev page
			if ([textView getStart] != currentcStart) //cstart is modified, thus can draw prev page
			{
				[textView drawSubRect:context inRect:rect];
				[textView scrollPage:Page_Down];//back to current page
			}
			else {
				CGContextClearRect(context, rect);
			}

			break;
		}
		case KNEXT_PAGE:
		{
			int currentcStart = [textView getStart];
			[textView scrollPage:Page_Down]; //try to draw prev page
			if ([textView getStart] != currentcStart) //cstart is modified, thus can draw prev page
			{
				[textView drawSubRect:context inRect:rect];
				[textView scrollPage:Page_Up];//back to current page
			}
			else {
				CGContextClearRect(context, rect);
			}
			break;
		}
	}
	return [super drawRect:rect];
}


- (void)dealloc {
    [super dealloc];
}


@end
